{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 直线回归相关系数及其计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 案例\n",
    "\n",
    "计算身高与前臂长之间的相关系数\n",
    "\n",
    "### 数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (11, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>IID</th><th>height</th><th>length</th></tr><tr><td>i64</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>1</td><td>170</td><td>47</td></tr><tr><td>2</td><td>173</td><td>42</td></tr><tr><td>3</td><td>160</td><td>44</td></tr><tr><td>4</td><td>155</td><td>41</td></tr><tr><td>5</td><td>173</td><td>47</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>7</td><td>178</td><td>47</td></tr><tr><td>8</td><td>183</td><td>46</td></tr><tr><td>9</td><td>180</td><td>49</td></tr><tr><td>10</td><td>165</td><td>43</td></tr><tr><td>11</td><td>166</td><td>44</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (11, 3)\n",
       "┌─────┬────────┬────────┐\n",
       "│ IID ┆ height ┆ length │\n",
       "│ --- ┆ ---    ┆ ---    │\n",
       "│ i64 ┆ i64    ┆ i64    │\n",
       "╞═════╪════════╪════════╡\n",
       "│ 1   ┆ 170    ┆ 47     │\n",
       "│ 2   ┆ 173    ┆ 42     │\n",
       "│ 3   ┆ 160    ┆ 44     │\n",
       "│ 4   ┆ 155    ┆ 41     │\n",
       "│ 5   ┆ 173    ┆ 47     │\n",
       "│ …   ┆ …      ┆ …      │\n",
       "│ 7   ┆ 178    ┆ 47     │\n",
       "│ 8   ┆ 183    ┆ 46     │\n",
       "│ 9   ┆ 180    ┆ 49     │\n",
       "│ 10  ┆ 165    ┆ 43     │\n",
       "│ 11  ┆ 166    ┆ 44     │\n",
       "└─────┴────────┴────────┘"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars as pl\n",
    "\n",
    "df = pl.read_csv(\"B_11_3-data.csv\")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r = 0.80087\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import linregress\n",
    "\n",
    "res = linregress(df.select(\"length\").to_series(), df.select(\"height\").to_series())\n",
    "\n",
    "print(f\"r = {float(res.rvalue):.5f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分析\n",
    "\n",
    "由结果可知，$ r \\geqslant 0 $，表示前臂长与身高呈正相关，且有很高的相关性。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
